package com.seeyon.apps.ext.ObxMsgPush.utils;

import com.seeyon.apps.weixin.util.WeixinUtil;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.SystemEnvironment;
import com.seeyon.ctp.common.constants.ApplicationCategoryEnum;
import com.seeyon.ctp.common.constants.Constants;
import com.seeyon.ctp.common.i18n.ResourceUtil;
import com.seeyon.ctp.common.usermessage.UserMessageUtil;
import com.seeyon.ctp.common.usermessage.pipeline.Message;
import com.seeyon.ctp.link.biz.ShortChainInterface;
import com.seeyon.ctp.login.online.OnlineRecorder;
import com.seeyon.ctp.login.online.OnlineUser;
import com.seeyon.ctp.organization.OrgConstants;
import com.seeyon.ctp.util.Datetimes;
import com.seeyon.ctp.util.LightWeightEncoder;
import com.seeyon.ctp.util.Strings;
import com.seeyon.ctp.util.json.JSONUtil;

import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ObxMessageUtil {
    private static Log log = LogFactory.getLog(MessageUtil.class);

    private static final String onlinePushCheck = AppContext.getSystemProperty("obxmsgpush.checkrule.push");

    /**
     * A:在线时推送 B:在线时不推送
     *
     * @return
     */
    public static boolean checkRuleOnline() {
        return onlinePushCheck.equals("B");
    }

    /**
     * 检查是否在线
     * @param onlineUser
     * @return
     */
    public static boolean checkOnlineUser(OnlineUser onlineUser) {
        if (onlineUser == null) {
            return true;
        }
        Set<String> loginTypes = onlineUser.getLoginTypes();
        if (loginTypes != null && (loginTypes.contains(Constants.login_useragent_from.pc.name()) || loginTypes
                .contains(Constants.login_useragent_from.iphone.name()) || loginTypes
                .contains(Constants.login_useragent_from.androidphone.name()) || loginTypes
                .contains(Constants.login_useragent_from.ipad.name()) || loginTypes
                .contains(Constants.login_useragent_from.androidpad.name()))) {
            return false;
        }
        return true;

    }

    /**
     * 获取消息链接
     * @param message 消息对象
     * @param platform 标识
     * @param corpid 企业ID
     * @return
     */
    public static String getMessageJson(Message message, String platform, String corpid) {
        String messageJson = "";
        try {
            boolean isSendMessage = true;
            if (message.getReceiverMember().getExternalType().intValue() != OrgConstants.ExternalType.Inner.ordinal()) {
                isSendMessage = false;
            }
            //开启检查 在线不推送时
            if (checkRuleOnline()) {
                //获取在线用户 ,兼容有些版本,获取的是列表的问题
                try {
                    OnlineUser onlineUser = (OnlineUser) OnlineRecorder.getOnlineUser(message.getReceiverMember().getLoginName());
                    isSendMessage = checkOnlineUser(onlineUser);
                    log.info("单用户不满足推送条件...返回数据" + isSendMessage);
                } catch (Exception e) {
                    List<OnlineUser> onlineUsers = OnlineRecorder.getOnlineUser(message.getReceiverMember().getLoginName());
                    for (OnlineUser onlineUser : onlineUsers) {
                        isSendMessage = checkOnlineUser(onlineUser);
                        log.info("多用户不满足推送条件...返回数据");
                    }
                    log.error("push  Obx check msg Error!", e);
                }
            }

            if (!isSendMessage) {
                log.info("不满足推送条件...返回数据");
                return "";
            }
            if (message.getCategory() == ApplicationCategoryEnum.collaboration.key() || message
                    .getCategory() == ApplicationCategoryEnum.templateApprove.key() || message
                    .getCategory() == ApplicationCategoryEnum.edoc.key() || message
                    .getCategory() == ApplicationCategoryEnum.edocSend.key() || message
                    .getCategory() == ApplicationCategoryEnum.edocRec.key() || message
                    .getCategory() == ApplicationCategoryEnum.edocSign.key() || message
                    .getCategory() == ApplicationCategoryEnum.exSend.key() || message
                    .getCategory() == ApplicationCategoryEnum.exSign.key() || message
                    .getCategory() == ApplicationCategoryEnum.edocRegister.key() || message
                    .getCategory() == ApplicationCategoryEnum.edocRecDistribute.key() || message
                    .getCategory() == ApplicationCategoryEnum.exchange.key() || message
                    .getCategory() == ApplicationCategoryEnum.news.key() || message
                    .getCategory() == ApplicationCategoryEnum.bulletin.key() || message
                    .getCategory() == ApplicationCategoryEnum.bbs.key() || message
                    .getCategory() == ApplicationCategoryEnum.show.key() || message
                    .getCategory() == ApplicationCategoryEnum.inquiry.key() || message
                    .getCategory() == ApplicationCategoryEnum.calendar.key() || message
                    .getCategory() == ApplicationCategoryEnum.taskManage.key() || message
                    .getCategory() == ApplicationCategoryEnum.meeting.key() || message
                    .getCategory() == ApplicationCategoryEnum.attendance.key() || message
                    .getCategory() == ApplicationCategoryEnum.office.key() || message
                    .getCategory() == ApplicationCategoryEnum.ai.key() || message
                    .getCategory() == ApplicationCategoryEnum.vreport.key() || message
                    .getCategory() > 2000) {
                if (WeixinUtil.isLink()) {
                    messageJson = getMessage(message, "link", platform);
                } else {
                    messageJson = getMessage(message, "text", platform);
                }
            } else if (message.getCategory() == ApplicationCategoryEnum.doc.key() || message
                    .getCategory() == ApplicationCategoryEnum.plan.key() || message
                    .getCategory() == ApplicationCategoryEnum.behavioranalysis.key() || message
                    .getCategory() == ApplicationCategoryEnum.hr.key()) {
                messageJson = getMessage(message, "text", platform);
            }
        } catch (Exception e) {
            log.error("push Obx msg Error!", e);
        }
        return messageJson;
    }

    /**
     * 获取消息地址
     * @param message 消息对象
     * @param messageType 消息类型
     * @param platform
     * @return
     */
    private static String getMessage(Message message, String messageType, String platform) {
        int category = message.getCategory();
        String remoteUrl = message.getRemoteURL();
        String content = message.getContent();
        String linkType = message.getLinkType();
        Long id = message.getReferenceId();
        if (id != null &&
                id.longValue() == -1L && message.getLinkParams() != null && (message.getLinkParams()).length > 0)
            id = Long.valueOf(NumberUtils.toLong(message.getLinkParams()[0], -1L));
        if ("link".equals(messageType) && Strings.isNotBlank(linkType)) {
            if (category == ApplicationCategoryEnum.templateApprove.key()) {
                String path = "";
                path = SystemEnvironment.getContextPath() + "/m3/apps/v5/templateapprove/html/dealWithTemplateApprove.html?state=" + message.getLinkParams()[0] + "&objectId=" + message.getLinkParams()[1] + "&templateId=" + message.getLinkParams()[2] + "&affairId=" + message.getLinkParams()[3] + "&formId=" + message.getLinkParams()[4];
                remoteUrl = dealWithUrl(path, platform, corpid, message);
            } else if (category == ApplicationCategoryEnum.collaboration.key()) {
                String path = "";
                if ("message.link.col.supervise".equals(linkType) || "message.link.col.traceRecord"
                        .equals(linkType) || "message.link.formtrigger.msg.flow"
                        .equals(linkType) || "message.link.formtrigger.msg.unflow"
                        .equals(linkType) || "message.link.formtrigger.cap4.msg.unflow"
                        .equals(linkType)) {
                    content = Strings.isBlank(content) ? ResourceUtil.getString("weixin.login.processing.please") : (content + " " + ResourceUtil.getString("weixin.login.processing.please"));
                    remoteUrl = "";
                } else {
                    path = SystemEnvironment.getContextPath() + "/m3/apps/v5/collaboration/html/details/summary.html?affairId=" + id + "&openFrom=listPending&summaryId=-1&proxyId=0";
                    remoteUrl = dealWithUrl(path, platform, corpid, message);
                }
            } else if (category == ApplicationCategoryEnum.edoc.key() || category == ApplicationCategoryEnum.edocSend
                    .key() || category == ApplicationCategoryEnum.edocRec
                    .key() || category == ApplicationCategoryEnum.edocSign
                    .key() || category == ApplicationCategoryEnum.exSend
                    .key() || category == ApplicationCategoryEnum.exSign
                    .key() || category == ApplicationCategoryEnum.edocRegister
                    .key() || category == ApplicationCategoryEnum.edocRecDistribute
                    .key() || category == ApplicationCategoryEnum.exchange
                    .key()) {
                String path = "";
                if ("message.link.edoc.supervise.detail".equals(linkType) || "message.link.edoc.supervise.main"
                        .equals(linkType) || "message.link.exchange.distribute"
                        .equals(linkType) || "message.link.exchange.receive"
                        .equals(linkType) || "message.link.exchange.receive.hasten"
                        .equals(linkType) || "message.link.exchange.register.govpending"
                        .equals(linkType) || "message.link.exchange.register.pending"
                        .equals(linkType) || "message.link.exchange.register.receive"
                        .equals(linkType) || "message.link.exchange.registered"
                        .equals(linkType) || "message.link.exchange.send"
                        .equals(linkType) || "message.link.exchange.sent"
                        .equals(linkType) || "message.link.edoc.traceRecord"
                        .equals(linkType) || "message.link.govdoc.traceRecord"
                        .equals(linkType) || "message.link.edoc.supervise"
                        .equals(linkType)) {
                    content = Strings.isBlank(content) ? ResourceUtil.getString("weixin.login.processing.please") : (content + " " + ResourceUtil.getString("weixin.login.processing.please"));
                    remoteUrl = "";
                } else if (Strings.isNotBlank(linkType) && (linkType.contains("govdoc") || linkType.contains("ocip"))) {
                    id = Long.valueOf(message.getLinkParams()[0]);
                    path = SystemEnvironment.getContextPath() + "/m3/apps/v5/edoc/html/details/summary.html?affairId=" + id + "&openFrom=listPending&summaryId=-1&proxyId=0";
                    if ("message.link.govdoc.supervise".equals(linkType))
                        path = SystemEnvironment.getContextPath() + "/m3/apps/v5/edoc/html/details/summary.html?affairId=-1&openFrom=edocStatistics&summaryId=" + id + "&proxyId=0";
                    remoteUrl = dealWithUrl(path, platform, corpid, message);
                } else {
                    id = Long.valueOf(message.getLinkParams()[0]);
                    path = SystemEnvironment.getContextPath() + "/m3/apps/v5/edoc/html/edocSummary.html?affairId=" + id + "&openFrom=listPending&summaryId=-1&proxyId=0";
                    remoteUrl = dealWithUrl(path, platform, corpid, message);
                }
            } else if (category == ApplicationCategoryEnum.meeting.key()) {
                String path = "";
                if ("message.link.mt.room_perm".equals(linkType) || "message.link.office.meetingroom"
                        .equals(linkType)) {
                    content = Strings.isBlank(content) ? ResourceUtil.getString("weixin.login.processing.please") : (content + " " + ResourceUtil.getString("weixin.login.processing.please"));
                    remoteUrl = "";
                } else if ("message.link.mt.summary".equals(linkType) || "message.link.mt.summary_send"
                        .equals(linkType) || "message.link.mt.send.scope"
                        .equals(linkType)) {
                    path = SystemEnvironment.getContextPath() + "/m3/apps/v5/meeting/html/meetingSummary.html?meetingSummaryId=" + id + "&openFrom=message&affairId=-1&proxyId=0&proxy=false";
                    remoteUrl = dealWithUrl(path, platform, corpid, message);
                } else {
                    path = SystemEnvironment.getContextPath() + "/m3/apps/v5/meeting/html/meetingDetail.html?meetingId=" + id + "&openFrom=pending&affairId=-1&proxyId=0&proxy=false";
                    remoteUrl = dealWithUrl(path, platform, corpid, message);
                }
            } else if (category == ApplicationCategoryEnum.news.key()) {
                String path = SystemEnvironment.getContextPath() + "/m3/apps/v5/news/html/newsView.html?newsId=" + id + "&comeFrom=0";
                if ("message.link.news.writedetail".equals(linkType)) {
                    content = Strings.isBlank(content) ? ResourceUtil.getString("weixin.login.processing.please") : (content + " " + ResourceUtil.getString("weixin.login.processing.please"));
                    remoteUrl = "";
                } else {
                    remoteUrl = dealWithUrl(path, platform, corpid, message);
                }
            } else if (category == ApplicationCategoryEnum.bulletin.key()) {
                String path = SystemEnvironment.getContextPath() + "/m3/apps/v5/bulletin/html/bulView.html?bulId=" + id + "&comeFrom=0";
                if ("message.link.bul.writedetail".equals(linkType)) {
                    content = Strings.isBlank(content) ? ResourceUtil.getString("weixin.login.processing.please") : (content + " " + ResourceUtil.getString("weixin.login.processing.please"));
                    remoteUrl = "";
                } else {
                    remoteUrl = dealWithUrl(path, platform, corpid, message);
                }
            } else if (category == ApplicationCategoryEnum.bbs.key()) {
                String path = SystemEnvironment.getContextPath() + "/m3/apps/v5/bbs/html/bbsView.html?bbsId=" + id + "&from=message";
                remoteUrl = dealWithUrl(path, platform, corpid, message);
            } else if (category == ApplicationCategoryEnum.inquiry.key()) {
                String path = "";
                if ("message.link.inq.alreadyauditing.pass".equals(linkType) || "message.link.inq.alreadyauditing.nopass"
                        .equals(linkType)) {
                    content = Strings.isBlank(content) ? ResourceUtil.getString("weixin.login.processing.please") : (content + " " + ResourceUtil.getString("weixin.login.processing.please"));
                    remoteUrl = "";
                } else if ("message.link.inq.auditing".equals(linkType)) {
                    path = SystemEnvironment.getContextPath() + "/m3/apps/v5/inquiry/html/inquiryView.html?inquiryId=" + id + "&comeFrom=2&affairState=4";
                    remoteUrl = dealWithUrl(path, platform, corpid, message);
                } else {
                    path = SystemEnvironment.getContextPath() + "/m3/apps/v5/inquiry/html/inquiryView.html?inquiryId=" + id + "&comeFrom=0";
                    remoteUrl = dealWithUrl(path, platform, corpid, message);
                }
            } else if (category == ApplicationCategoryEnum.vreport.key()) {
                if ("message.link.vreport.schstats.report".equals(linkType)) {
                    String statsAppCategory = message.getLinkParams()[2];
                    String isSucess = message.getLinkParams()[4];
                    if ("true".equals(isSucess)) {
                        StringBuilder path = null;
                        if (ApplicationCategoryEnum.cap4biz.name().equals(statsAppCategory)) {
                            path = new StringBuilder(SystemEnvironment.getContextPath() + "/m3/apps/v5/vreport/html/capReportBridge.html?");
                            path.append("designId=").append(message.getLinkParams()[0]);
                            path.append("&schlogId=").append(message.getLinkParams()[1]);
                            path.append("&category=").append(message.getLinkParams()[2]);
                            path.append("&categoryId=").append(message.getLinkParams()[3]);
                            path.append("&success=").append(message.getLinkParams()[4]);
                        } else if (ApplicationCategoryEnum.global.name().equals(statsAppCategory)) {
                            path = new StringBuilder(SystemEnvironment.getContextPath() + "/m3/apps/v5/vreport/html/reportIndex.html?");
                            path.append("designId=").append(message.getLinkParams()[0]);
                            path.append("&schlogId=").append(message.getLinkParams()[1]);
                            path.append("&category=").append(message.getLinkParams()[2]);
                            path.append("&categoryId=").append(message.getLinkParams()[3]);
                            path.append("&success=").append(message.getLinkParams()[4]);
                        }
                        if (path != null)
                            remoteUrl = dealWithUrl(path.toString(), platform, corpid, message);
                    }
                }
            } else if (category == ApplicationCategoryEnum.show.key()) {
                String showbarId = message.getLinkParams()[0];
                StringBuilder path = new StringBuilder(SystemEnvironment.getContextPath() + "/m3/apps/v5/show/html/showbarDetail.html?id=");
                path.append(showbarId);
                if ("message.link.show.showReply".equals(linkType)) {
                    content = content.replaceAll("\"", "");
                    path.append("&showpostId=").append(message.getLinkParams()[1]);
                    path.append("&commentId=").append(message.getLinkParams()[2]);
                    path.append("&from=").append(message.getLinkParams()[3]);
                } else if ("message.link.show.newshowbar".equals(linkType)) {
                    path.append("&from=").append(message.getLinkParams()[1]);
                } else if ("message.link.show.showpost.settop".equals(linkType) || "message.link.show.showPraise"
                        .equals(linkType)) {
                    path.append("&showpostId=").append(message.getLinkParams()[1]);
                    path.append("&from=").append(message.getLinkParams()[2]);
                }
                path.append("&openType=").append("wechat");
                remoteUrl = dealWithUrl(path.toString(), platform, corpid, message);
            } else if (category == ApplicationCategoryEnum.attendance.key()) {
                String path = "";
                if ("message.link.attendance.view".equals(linkType)) {
                    path = SystemEnvironment.getContextPath() + "/m3/apps/v5/attendance/html/attendanceIndex.html?from=message";
                } else {
                    path = SystemEnvironment.getContextPath() + "/m3/apps/v5/attendance/html/attendanceDetail.html?attendanceId=" + id;
                }
                remoteUrl = dealWithUrl(path, platform, corpid, message);
            } else if (category == ApplicationCategoryEnum.taskManage.key()) {
                StringBuilder path = new StringBuilder(SystemEnvironment.getContextPath() + "/m3/apps/v5/taskmanage/html/");
                if ("message.link.taskmanage.viewfeedback".equals(linkType)) {
                    path.append("taskFeedbackList.html?openType=wechat&taskId=").append(message.getLinkParams()[0]);
                } else if ("message.link.taskmanage.status".equals(linkType)) {
                    String[] linkParams = message.getLinkParams();
                    path.append("task_index.html?openType=wechat&listType=" + linkParams[0] + "&status=overdue");
                } else {
                    path.append("taskEditor.html?openType=wechat&taskId=").append(message.getLinkParams()[0]);
                }
                remoteUrl = dealWithUrl(path.toString(), platform, corpid, message);
            } else if (category == ApplicationCategoryEnum.calendar.key()) {
                StringBuilder path = new StringBuilder(SystemEnvironment.getContextPath() + "/m3/apps/v5/calendar/html/newCalEvent.html?");
                path.append("id=").append(message.getLinkParams()[0]);
                remoteUrl = dealWithUrl(path.toString(), platform, corpid, message);
            } else if (category == ApplicationCategoryEnum.office.key()) {
                Long affairId = Long.valueOf(-1L);
                if (message.getLinkParams() != null && (message.getLinkParams()).length > 0)
                    affairId = Long.valueOf(NumberUtils.toLong(message.getLinkParams()[0], -1L));
                String path = SystemEnvironment.getContextPath() + "/m3/apps/v5/office/html/";
                if ("message.link.office.autoN.audit".equals(linkType)) {
                    path = path + "audit_auto.html?affairId=" + affairId;
                    remoteUrl = dealWithUrl(path, platform, corpid, message);
                } else if ("message.link.office.stockN.audit".equals(linkType)) {
                    path = path + "audit_stock.html?affairId=" + affairId;
                    remoteUrl = dealWithUrl(path, platform, corpid, message);
                } else if ("message.link.office.assetN.audit".equals(linkType)) {
                    path = path + "audit_asset.html?affairId=" + affairId;
                    remoteUrl = dealWithUrl(path, platform, corpid, message);
                } else if ("message.link.office.bookN.audit".equals(linkType)) {
                    path = path + "audit_book.html?applyId=" + id;
                    remoteUrl = dealWithUrl(path, platform, corpid, message);
                } else {
                    content = Strings.isBlank(content) ? ResourceUtil.getString("weixin.login.processing.please") : (content + " " + ResourceUtil.getString("weixin.login.processing.please"));
                    remoteUrl = "";
                }
            } else if (category > 2000) {
                if (message.getLinkParams() != null && (message.getLinkParams()).length > 0) {
                    String isWeixinLink = message.getLinkParams()[5];
                    if (Strings.isBlank(isWeixinLink))
                        isWeixinLink = message.getLinkParams()[4];
                    if ("1".equals(isWeixinLink)) {
                        String thirdId = message.getLinkParams()[1];
                        String registerCode = message.getLinkParams()[2];
                        if (StringUtils.isBlank(registerCode)) {
                            remoteUrl = "";
                        } else if (registerCode.startsWith("http")) {
                            remoteUrl = registerCode;
                            remoteUrl = remoteUrl + (remoteUrl.contains("?") ? "&weixinMessage=true" : "?weixinMessage=true");
                        } else {
                            StringBuilder sb = new StringBuilder(SystemEnvironment.getContextPath() + "/m3/apps/v5/cip/html/cipWeixinMessage.html?thirdpartyMessageId=");
                            sb.append(thirdId);
                            sb.append("&registerCode=");
                            sb.append(registerCode);
                            remoteUrl = dealWithUrl(sb.toString(), platform, corpid, message);
                        }
                    } else {
                        remoteUrl = "";
                    }
                } else {
                    remoteUrl = "";
                }
            } else if (category == ApplicationCategoryEnum.ai.key()) {
                String path = "";
                if ("message.link.col.pending".equals(linkType)) {
                    path = SystemEnvironment.getContextPath() + "/m3/apps/v5/collaboration/html/details/summary.html?affairId=" + id + "&openFrom=listPending&summaryId=-1&proxyId=0";
                    remoteUrl = dealWithUrl(path, platform, corpid, message);
                }
            }
        } else {
            remoteUrl = "";
        }
        Long receiverId = message.getReceiverMember().getId();
        Long senderId = Long.valueOf(message.getSenderMemberId());
        String sendUserName = "";
        if (senderId.longValue() == -1L) {
            if (Constants.UserMessage_SOURCE_TYPE.AI.ordinal() == message.getSourceType()) {
                sendUserName = ResourceUtil.getString("application.72.label");
            } else {
                sendUserName = ResourceUtil.getString("anonymous.label");
            }
        } else {
            sendUserName = message.getSenderMember().getName();
        }
        HashMap<Object, Object> messageMap = new HashMap<>();
        messageMap.put("D", Datetimes.formatDatetime(message.getCreateDate()));
        messageMap.put("S", sendUserName);
        messageMap.put("R", String.valueOf(receiverId));
        messageMap.put("C", content);
        messageMap.put("U", remoteUrl);
        messageMap.put("M", String.valueOf(category));
        messageMap.put("L", linkType);
        messageMap.put("I", WeixinUtil.getOaUrl());
        String messagePoJsonStr = JSONUtil.toJSONString(messageMap);
        return messagePoJsonStr;
    }

    private static String dealWithUrl(String url, String platform, String corpid, Message message) {
        String messageUrl = "";
        try {
            url = url + "&weixinMessage=true";
            if ("weixin".equals(platform)) {
                String oaUrl = WeixinUtil.getOaUrl();
                URL url1 = new URL(oaUrl);
                messageUrl = WeixinUtil.getIp() + "/content/viewh5Message?url=" + URLEncoder.encode(LightWeightEncoder.encodeString(oaUrl + "" + (Strings.isBlank(url1.getPath()) ? SystemEnvironment.getContextPath() : "") + "/H5/collaboration/messageIndex.html?html=" + url + "&pcUrl=" + getRemoteURL(message)), "UTF-8");
            } else if ("dingding".equals(platform)) {
                String redirect_uri = URLEncoder.encode(WeixinUtil.getOaUrl() + SystemEnvironment.getContextPath() + "/wechat/dingding.do?method=viewh5Message&html=" + URLEncoder.encode(url, "UTF-8"), "UTF-8");
                messageUrl = "https://oapi.dingtalk.com/connect/oauth2/authorize?appid=" + corpid + "&redirect_uri=" + redirect_uri + "&response_type=code&scope=snsapi_base&state=" + corpid;
            }
        } catch (Exception e) {
            log.error("", e);
        }
        return messageUrl;
    }

    private static String getURLPrefix() {
        String oaUrl = WeixinUtil.getOaUrl();
        if (Strings.isNotBlank(oaUrl))
            return oaUrl + SystemEnvironment.getContextPath() + "/thirdpartyController.do?method=access";
        return "";
    }

    private static String getRemoteURL(Message message) {
        String _URLPrefix = getURLPrefix();
        String link = (message.getLinkType() == null) ? null : (String) UserMessageUtil.getMessageLinkType().get(message.getLinkType());
        if (Strings.isBlank(link) || link.toLowerCase().startsWith("javascript:") || Strings.isBlank(_URLPrefix))
            return null;
        StringBuilder url = new StringBuilder();
        url.append(_URLPrefix);
        String enc = LightWeightEncoder.encodeString("MID=" + message.getUserHistoryMessageId() + "&L=" + message.getLinkType() + "&T=" + message.getCreateDate().getTime());
        ShortChainInterface shortChainInterface = (ShortChainInterface) AppContext.getBean("shortChainInterface");
        String receiverId = message.getReceiverMember().getId().toString();
        String chainId = shortChainInterface.generateChain(enc, receiverId);
        url.append("&chainId=" + chainId);
        if (message.getLinkParams() != null)
            for (String string : message.getLinkParams())
                url.append("&P=" + string);
        return url.toString();
    }
}
